package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;



import data.POS;
import data.POSTable;
import data.POSZone;

public class POSTableLogic extends AbstractBL {
	public POSTableLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public List<POSTable> GenerateTable(long hotelId)
	{
		List<POSTable> list = new ArrayList<POSTable>(); 
		POS p=new POSLogic(req,resp).GetPOS(hotelId,"iChic");
		POSZone z = GetPOSZone(hotelId,"karaoke",p.getId());
		POSZone a = GetPOSZone(hotelId,"zoneA",p.getId());
		POSZone b = GetPOSZone(hotelId,"zoneB",p.getId());
		POSTable c;
		if(GetPOSTable(hotelId,"01") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("01");
			c.setZoneString(String.valueOf(z.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"02") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("02");
			c.setZoneString(String.valueOf(a.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"03") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("03");
			c.setZoneString(String.valueOf(z.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"04") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("04");
			c.setZoneString(String.valueOf(z.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"05") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("05");
			c.setZoneString(String.valueOf(a.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"06") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("06");
			c.setZoneString(String.valueOf(b.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"07") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("07");
			c.setZoneString(String.valueOf(a.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"08") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("08");
			c.setZoneString(String.valueOf(z.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"09") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("09");
			c.setZoneString(String.valueOf(b.getId()));
			list.add(c);
		}
		if(GetPOSTable(hotelId,"10") == null){
			c = new POSTable();
			c.setHotelId(hotelId);
			c.setTableTitle("10");
			c.setZoneString(String.valueOf(b.getId()));
			list.add(c);
		}
		return list;
	}
	public List<POSZone> GenerateZone(long hotelId)
	{
		List<POSZone> list = new ArrayList<POSZone>();
		
		POSZone c; 
		POS p=new POSLogic(req,resp).GetPOS(hotelId,"iChic");
		if(GetPOSZone(hotelId,"karaoke",p.getId()) == null){
			c = new POSZone();
			c.setHotelId(hotelId);
			c.setPOSId(p.getId());
			c.setTitle("karaoke");
			list.add(c);
		}
		if(GetPOSZone(hotelId,"zoneA",p.getId()) == null){
			c = new POSZone();
			c.setHotelId(hotelId);
			c.setPOSId(p.getId());
			c.setTitle("zoneA");
			list.add(c);
		}
		if(GetPOSZone(hotelId,"zoneB",p.getId()) == null){
			c = new POSZone();
			c.setHotelId(hotelId);
			c.setPOSId(p.getId());
			c.setTitle("zoneB");
			list.add(c);
		}
		
		return list;
	}
	public POSTable GetPOSTable(long id) {
		Key key = KeyFactory.createKey(EntityName.POSTable, id);
		try {
			Entity lang = datastore.get(key);
			POSTable c = new POSTable();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	public POSZone GetPOSZone(long id) {
		Key key = KeyFactory.createKey(EntityName.POSZone, id);
		try {
			Entity lang = datastore.get(key);
			POSZone c = new POSZone();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	public List<POSTable> GetAllPOSTable(long hotelId) {
		Query q = new Query(EntityName.POSTable);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable);
		q.setFilter(filter);
		return GetPOSTableList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSTable> GetAllPOSTableInZone(long hotelId,long zoneId) {
		Query q = new Query(EntityName.POSTable);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filterZone = new FilterPredicate("zone",
				FilterOperator.EQUAL, String.valueOf(zoneId));
		
		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable,filterZone);
		q.setFilter(filter);
		return GetPOSTableList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSTable> GetPOSTableList(List<Entity> entities) {
		List<POSTable> list = new ArrayList<POSTable>();
		if(entities!=null)
		for (Entity e : entities) {
			POSTable a = new POSTable();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public List<POSZone> GetAllPOSZone(long hotelId) {
		Query q = new Query(EntityName.POSZone);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable);
		q.setFilter(filter);
		return GetPOSZoneList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSZone> GetAllPOSZonebyPosId(long hotelId,long posId) {
		Query q = new Query(EntityName.POSZone);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterposId = new FilterPredicate("POSId",
				FilterOperator.EQUAL, posId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filter = CompositeFilterOperator.and(filterHotel,filterposId, filterStatus,
				filterAvaliable);
		q.setFilter(filter);
		return GetPOSZoneList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSZone> GetAllPOSZoneLimit(long hotelId,long offset,long line,String posId)
	{
		Query q = new Query(EntityName.POSZone);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterPosId = new FilterPredicate("POSId",FilterOperator.EQUAL,Long.parseLong(posId));
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable,filterPosId);
		q.setFilter(filter);
		return GetPOSZoneList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
		//return GetPOSUnitList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSTable> GetAllPOSTableInZoneLimit(long hotelId,long offset,long line,long zoneId) {
		Query q = new Query(EntityName.POSTable);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filterZone = new FilterPredicate("zone",
				FilterOperator.EQUAL, String.valueOf(zoneId));
		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable,filterZone);
		q.setFilter(filter);
		return GetPOSTableList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	
	public List<POSZone> GetAllPOSZoneSize(long hotelId,String posId) {
		Query q = new Query(EntityName.POSZone);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filterPosId = new FilterPredicate("POSId",FilterOperator.EQUAL,Long.parseLong(posId));
		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable,filterPosId);
		q.setFilter(filter);
		return GetPOSZoneList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSZone> GetPOSZoneList(List<Entity> entities) {
		List<POSZone> list = new ArrayList<POSZone>();
		if(entities!=null)
		for (Entity e : entities) {
			POSZone a = new POSZone();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public POSTable GetPOSTable(long hotelId, String tableTitle) {
		Query q = new Query(EntityName.POSTable);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filtertable = new FilterPredicate("tableTitle", FilterOperator.EQUAL,
				tableTitle);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable, filtertable);
		q.setFilter(filter);
		return GetPOSTable(datastore.prepare(q).asSingleEntity());
	}
	
	public POSTable GetPOSTable(Entity entity) {
		if(entity == null) return null;
		POSTable item = new POSTable();
		item.setEntity(entity);
		return item;
	}
	
	public POSZone GetPOSZone(long hotelId, String title,long POSid) {
		Query q = new Query(EntityName.POSZone);
		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterStatus = new FilterPredicate("status",
				FilterOperator.EQUAL, true);
		Filter filterAvaliable = new FilterPredicate("avaliable",
				FilterOperator.EQUAL, true);
		Filter filterzone = new FilterPredicate("title", FilterOperator.EQUAL,
				title);
		Filter filterPosId = new FilterPredicate("POSId", FilterOperator.EQUAL,
				POSid);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus,
				filterAvaliable, filterzone,filterPosId);
		q.setFilter(filter);
		return GetPOSZone(datastore.prepare(q).asSingleEntity());
	}
	
	public POSZone GetPOSZone(Entity entity) {
		if(entity == null) return null;
		POSZone item = new POSZone();
		item.setEntity(entity);
		return item;
	}
	
	public POSZone AddZone(long id,String title,long posId) throws EntityNotFoundException{
		POSZone a = new POSZone();
		a.setHotelId(id);
		a.setTitle(title);
		a.setPOSId(posId);
		a.setAvaliable(true);
		a.setStatus(true);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}

	public POSTable AddTable(long id,String zonename,String nametable,String description) throws EntityNotFoundException{
		POSTable a = new POSTable();
		a.setHotelId(id);
		a.setZoneString(zonename);
		a.setTableTitle(nametable);
		a.setAvaliable(true);
		a.setStatus(true);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public POSTable EditTable(long tableId,String title) throws EntityNotFoundException{
		POSTable a = GetPOSTable(tableId);
		a.setTableTitle(title);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		
		return a;
	}
	
	public POSZone EditZone(long zoneId,String title) throws EntityNotFoundException{
		POSZone a = GetPOSZone(zoneId);
		a.setTitle(title);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public POSZone ChangeStatus(long zoneId) throws EntityNotFoundException{
		POSZone a = GetPOSZone(zoneId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSTable ChangeStatusTable(long tableId) throws EntityNotFoundException{
		POSTable a = GetPOSTable(tableId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSZone DeleteZone(long categoriesId) throws EntityNotFoundException{
		POSZone a = GetPOSZone(categoriesId);
		a.setAvaliable(false);
		a.setStatus(false);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSTable DeleteTable(long tableId) throws EntityNotFoundException{
		POSTable a = GetPOSTable(tableId);
		a.setAvaliable(false);
		a.setStatus(false);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
}
